package org.helldorado.companybuilder;

import java.util.List;
import org.helldorado.companybuilder.R;

import com.haarman.supertooltips.ToolTip;
import com.haarman.supertooltips.ToolTipRelativeLayout;
import com.haarman.supertooltips.ToolTipView;
import com.haarman.supertooltips.ToolTipView.OnToolTipViewClickedListener;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class TIPDAdapter{

	private Context mContext;
	private View mCurrentView;
	private ToolTipRelativeLayout mToolTipLayout;
	
	public TIPDAdapter(Context ctx, View currentView) {
		mContext = ctx;
		mCurrentView = currentView;
		mToolTipLayout = (ToolTipRelativeLayout) currentView.findViewById(R.id.toolTip);
	}
	
//	private void addToolTip(final TextView textView, final TIPD tipd) { 
//		   View.OnClickListener myHandler2 = new View.OnClickListener() {
//			    public void onClick(View v) {
//
//			    	//ToolTipRelativeLayout mToolTipFrameLayout = (ToolTipRelativeLayout) currView.findViewById(R.id.activity_main_tooltipRelativeLayout);
//			  	  
//			    	OnToolTipViewClickedListener myHandlerTT = new OnToolTipViewClickedListener(){  
//					  public void onToolTipViewClicked(ToolTipView toolTipView) {
//						  toolTipView = null;
//					  }
//			    	};
//			    	TextView v0 = textView;
//			    	ToolTipView toolTipView = mToolTipLayout.showToolTipForView(
//			                new ToolTip()
//			                        .withText(tipd.getDescription())
//			                        .withColor(mContext.getResources().getColor(R.color.statLineBGColor))
//			                        .withShadow(true),
//			                        textView);
//			    	ToolTipManager.CloseOpenedToolTip(toolTipView);
//			        toolTipView.setOnToolTipViewClickedListener(myHandlerTT); 	  	  
//			    }
//			  };
//			  textView.setOnClickListener(myHandler2);
//	   }
//	private void AddTIPDToViewGroup(TIPD tipd, ViewGroup viewGroup ){
//		LayoutInflater inflater = LayoutInflater.from(mContext);
//		TextView tmp = (TextView) inflater.inflate(R.layout.textview_tooltip_layout, viewGroup, false);
//		tmp.setText(tipd.getTitle());
//		tmp.setId(viewGroup.getChildCount());
//		viewGroup.addView(tmp);		 
//		addToolTip(tmp, tipd);
//	}
//	
	public SpannableStringBuilder CreateTouchableSpanFromTIPD(TIPD tipd){
		String title = tipd.getTitle();
		SpannableStringBuilder ssb = new SpannableStringBuilder(title + "A");
		
		Drawable img = mContext.getResources().getDrawable(R.drawable.circle);
    	img.setBounds(0, 0, img.getIntrinsicWidth(), img.getIntrinsicHeight());
    	ssb.setSpan(new ImageSpan(img, ImageSpan.ALIGN_BASELINE), title.length(), title.length()+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		
		SpannableStringBuilder ssbFull = new SpannableStringBuilder(title);
		
    	ssbFull.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    	
    	String toolTipText = "\n\n";
		List<String> properties = tipd.getProperties();
		if (properties != null && properties.size() > 0)
		{
			String prefix = "";
			for (String prop : properties)
			{
				toolTipText = toolTipText + prefix + prop;
				prefix = " \u2022 ";
			}
			toolTipText = toolTipText + "\n\n";
		}
		toolTipText = toolTipText + tipd.getDescription();
		ssbFull.append(toolTipText);
		ssb.setSpan(
				  new TouchableSpanWithToolTip(
				ssbFull
				, mCurrentView
				, mContext.getResources().getColor(R.color.white))
				, 0
				, title.length()
				, 0);
		return ssb;
	}
	
	public void AddTIPDsToLayout(
			  List<TIPD> tipds
			, String identifier
			, ViewGroup parentLayout
			, String separator){
		AddTIPDsToLayout(tipds, identifier, parentLayout, separator, null);
	}
	public void AddTIPDsToLayout(
			  List<TIPD> tipds
			, String identifier
			, ViewGroup parentLayout
			, String separator
			, LayoutParams layoutParams){
		
		LayoutInflater inflater = LayoutInflater.from(mContext);
		TextView idTextView = (TextView) inflater.inflate(R.layout.tipds_textview_layout, (ViewGroup)parentLayout, false);

		SpannableStringBuilder info = new SpannableStringBuilder();
		info.append(identifier);
		info.setSpan(new StyleSpan(Typeface.BOLD), 0, identifier.length(), 0);
		String sep = " ";
		for (TIPD tipd : tipds){
			info.append(sep);
			info.append(CreateTouchableSpanFromTIPD(tipd));
			sep = separator;
		}

		idTextView.setMovementMethod(new LinkTouchMovementMethod());
		idTextView.setText(info, BufferType.SPANNABLE);
		if (layoutParams != null)
		{
			idTextView.setLayoutParams(layoutParams);
		}
		parentLayout.addView(idTextView);
	}
//public void AddTIPDRowToLayout(List<TIPD> tipds, String identifier, LinearLayout parentLayout){
//	  
//	  LayoutInflater inflater = LayoutInflater.from(mContext);
//	  LinearLayout row0 = (LinearLayout) inflater.inflate(R.layout.tipds_layout, (ViewGroup)parentLayout, false);
//	  LinearLayout row1 = null;
//	  
//	  TextView idTextView = (TextView) inflater.inflate(R.layout.tipds_textview_layout, (ViewGroup)row0, false);
//	  idTextView.setText(identifier);
//	  row0.addView(idTextView);
//	  int numTipds = tipds.size();
//	  if (numTipds < 6){
//		  for (TIPD tipd : tipds){
//			  AddTIPDToViewGroup(tipd, (ViewGroup) row0);
//		  }
//	  } else {
//		  // There will never be more than 10 tipds in Helldorado/Inferno
//		  // Split list in two lists
//		  
//		  List<TIPD> tipds0 = tipds.subList(0, numTipds/2);
//		  List<TIPD> tipds1 = tipds.subList(numTipds/2, numTipds);
//		  row1 = (LinearLayout) inflater.inflate(R.layout.tipds_layout, (ViewGroup)parentLayout, false);
//		  for (TIPD tipd : tipds0){
//			  AddTIPDToViewGroup(tipd, (ViewGroup) row0);
//		  }
//		  for (TIPD tipd : tipds1){
//			  AddTIPDToViewGroup(tipd, (ViewGroup) row1);
//		  }
//	  }
//		  
//	  parentLayout.addView(row0);
//	  if (row1 != null){
//		  parentLayout.addView(row1);
//	  }
//		  
//}

}
